﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Linq.Expressions;
using EntityFramework.Extensions;
using Abp.UI;
using Abp.Domain.Repositories;
using Omu.ValueInjecter;
using App.Goods.Dto;
using App.BaseObject;

namespace App.Goods
{
    public class GoodsAppService : AppServiceBase, IGoodsAppService
    {
        private readonly IRepository<Product> _goodsRepository;
        private readonly IRepository<ProductCategory> _categoryRepository;
        private readonly IRepository<ProductBrand> _brandRepository;

        public GoodsAppService(
            IRepository<Product> goodsRepository,
            IRepository<ProductCategory> categoryRepository,
            IRepository<ProductBrand> brandRepository)
        {
            _goodsRepository = goodsRepository;
            _categoryRepository = categoryRepository;
            _brandRepository = brandRepository;
        }

        public async Task CreateGoods(ProductDto dto)
        {
            if (await _goodsRepository.CountAsync(t => t.GoodsName == dto.GoodsName) > 0)
            {
                throw new UserFriendlyException(string.Format("名为【{0}】的商品已经存在！", dto.GoodsName));
            }
            var entity = new Product();
            entity.InjectFrom(dto);
            entity.TenantId = AbpSession.TenantId;
            entity.Brand = await _brandRepository.FirstOrDefaultAsync(s => s.Id == dto.BrandId);
            if (dto.GoodsCategorys != null)
            {
                entity.Categorys = new List<ProductCategory>();
                foreach (var categoryId in dto.GoodsCategorys)
                {
                    entity.Categorys.Add(await _categoryRepository.FirstOrDefaultAsync(s => s.Id == categoryId));
                }
            }

            await _goodsRepository.InsertAsync(entity);
        }


        public async Task UpdateGoods(int id, ProductDto dto)
        {
            var entity = await _goodsRepository.GetAllIncluding(s => s.Categorys).FirstOrDefaultAsync(s => s.Id == id);
            if (entity == null)
            {
                throw new UserFriendlyException(string.Format("找不到该记录Id为{0}的记录", id));
            }

            entity.InjectFrom(dto);
            entity.Brand = await _brandRepository.FirstOrDefaultAsync(s => s.Id == dto.BrandId);
            entity.Categorys.Clear();
            if (dto.GoodsCategorys != null)
                foreach (var item in dto.GoodsCategorys)
                {
                    entity.Categorys.Add(await _categoryRepository.FirstOrDefaultAsync(s => s.Id == item));
                }
        }

        public async Task CreateCategory(CategoryDto dto)
        {
            if (await _categoryRepository.CountAsync(t => t.CategoryName == dto.CategoryName) > 0)
            {
                throw new UserFriendlyException(string.Format("名为【{0}】的分类已经存在！", dto.CategoryName));
            }

            var entity = new ProductCategory();
            entity.InjectFrom(dto);
            entity.TenantId = AbpSession.TenantId;

            await _categoryRepository.InsertAsync(entity);
        }

        public async Task<List<ProductCategory>> GetCategorys(Expression<Func<ProductCategory, bool>> predicate = null)
        {
            if (predicate == null) return await _categoryRepository.GetAllListAsync();
            return await _categoryRepository.GetAllListAsync(predicate);
        }

        public async Task SaveBrands(BulkDto<BrandDto> dto)
        {
            //批量插入
            foreach (var row in dto.InsertedRows)
            {
                if (await _brandRepository.CountAsync(b => b.BrandName == row.BrandName) > 0)
                {
                    throw new UserFriendlyException(string.Format("名为【{0}】的品牌已经存在！", row.BrandName));
                }
                else
                {
                    var entity = new ProductBrand();
                    entity.InjectFrom(row);
                    entity.TenantId = AbpSession.TenantId;
                    await _brandRepository.InsertAsync(entity);
                }
            }
            //批量更新
            foreach (var row in dto.UpdatedRows)
            {
                var entity = await _brandRepository.SingleAsync(s => s.Id == row.Id);
                entity.InjectFrom(row);
            }

            //删除
            //删除
            foreach (var row in dto.DeletedRows)
            {
                if (row.Id.HasValue)
                    await _brandRepository.DeleteAsync(row.Id.Value);
            }
        }

        public async Task UpdateCategory(int id, CategoryDto dto)
        {
            var entity = await _categoryRepository.FirstOrDefaultAsync(s => s.Id == id);
            if (entity == null)
            {
                new UserFriendlyException(string.Format("找不到该记录Team Id为{0}的记录", id));
            }

            entity.InjectFrom(dto);
        }

        public async Task<Product> GetGoods(int id)
        {
            return await _goodsRepository.GetAllIncluding(s => s.Brand, s => s.Categorys).FirstOrDefaultAsync(s => s.Id == id);
        }

        public async Task DeleteAsync(int[] idList)
        {
            await _goodsRepository.GetAll().Where(s => idList.Contains(s.Id)).DeleteAsync();
        }

        public async Task DeleteCategoryAsync(int id)
        {
            await _categoryRepository.GetAll().Where(s => s.Id == id).DeleteAsync();
        }
    }
}
